<?php

namespace RHX\Helper;

class Number {
    /**
     * Convert number in roman number
     * @created    2007-01-21
     * @author    Jerome IZARD
     * @version    1.0
     * @param integer the number
     * @return string the roman number
     */
    public static function getRomanNumber($number) {
	$basicRomanNumbers	 = array('M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I');
	$basicNumbers		 = array(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1);

	// a number > 4999 can't be write in roman numbers
	if ($number > 4999)
	    $number = 4999;

	// in roman numbers 0 don't exist
	if ($number <= 0)
	    $number = 1;

	// decimal number can't be write in roman numbers
	$number = (int) $number;

	// initialisation of boucle's variables
	$romanNumber	 = '';
	$i		 = 0;

	while ($number > 0) {
	    if ($number >= $basicNumbers[$i]) {
		$number		 = $number - $basicNumbers[$i];
		$romanNumber	 = $romanNumber . $basicRomanNumbers[$i];
	    }
	    else {
		$i++;
	    }
	}

	return $romanNumber;
    }

    /**
     * Arrondi supéreur dans les chiffres à virgule
     *
     * @param float $floatVal
     * @param int $precision
     * @return float
     */
    public static function round($floatVal, $precision = 2) {
	if (is_string($floatVal)) {
	    $val		 = str_replace(',', '.', $floatVal);
	    $floatVal	 = floatval($val);
	}
	if (is_nan($floatVal)) {
	    return 0;
	}

	return round($floatVal, $precision);
    }
    
    /**
     * Conversion en Nombre réel
     * 
     * @param string $number
     * @return float
     */
    public static function parseFloat($number){
	if (is_string($number)) {
	    $val		 = str_replace(',', '.', $number);
	    $number	 = floatval($val);
	}
	if (is_nan($number)) {
	    return 0;
	}
	return $number;
    }


    /**
     * Conversion au format francophone
     * 
     * @param string|float $number
     * @param int $round
     * @return string
     */
    public static function frenchFormat($number, $round = 2){
	if(is_string($number)){
	    $val = str_replace(',', '.', $number);
	    $number = floatval($val);
	}
	if(is_nan($number)){
	    return '0,00';
	}
	return number_format($number, $round, ',', ' ');
    }
    
    /**
     * Libellé du site sans Numéro
     * 
     * @param string $libelle
     * @return string
     */
    public static function getSiteLibelle($libelle){
	list($id, $label) = sscanf($libelle, "%d - %s");
	return $label;
    }
    
    /**
     * Les deux chiffre de l'année de la période
     * 
     * @param string $libelle
     * @return string
     */
    public static function getPeriodeYear($libelle){
	list($libelle, $year) = sscanf($libelle, "%s %s");
	return substr($year, 2, 2);
    }

}

?>